﻿using Newtonsoft.Json;
using System.Globalization;
using System.Text.Json.Serialization;

namespace MiniFox.Infrastructure
{
    /// <summary>
    /// 标准响应报文
    /// </summary>
    [JsonObject]
    public class Response : IResponse
    {
        const string NO_ERR_CODE = "00000";
        #region head
        CultureInfo _culture;
        string _errorCode;
        /// <summary>
        /// 
        /// </summary>
        public Response() : this(CultureInfo.CurrentCulture)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cultureInfo"></param>
        public Response(CultureInfo cultureInfo)
        {
            _culture = cultureInfo;
            _errorCode = NO_ERR_CODE;
        }
        /// <summary>
        /// 错误代码
        /// </summary>
        [JsonProperty("code")]
        public string Code
        {
            get { return _errorCode; }
            set { _errorCode = value; }
        }
        /// <summary>
        /// 获取当前用于请求的culture对象(适用于全球化及多语言处理)
        /// </summary>
        protected CultureInfo Culture
        {
            get { return _culture; }
        }

        /// <summary>
        /// 响应消息说明
        /// </summary>
        [JsonProperty("message")]
        public string Message
        {
            get;
            set;
        }

        #endregion
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("success")]
        public bool Success
        {
            get
            {
                return this.Code == NO_ERR_CODE;
            }
        }
    }


    /// <summary>
    /// 一般响应
    /// </summary>
    [JsonObject]
    public class Response<T> : Response
    {
        /// <summary>
        /// 
        /// </summary>
        public Response() : this(CultureInfo.CurrentCulture)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cultureInfo"></param>
        public Response(CultureInfo cultureInfo) : base(cultureInfo)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public Response(T data) : this(data, CultureInfo.CurrentCulture)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="cultureInfo"></param>
        public Response(T data, CultureInfo cultureInfo) : base(cultureInfo)
        {
            Data = data;
        }
        /// <summary>
        /// 响应结果(业务模型或视图模型)
        /// </summary>
        [JsonProperty("data")]
        public T Data { get; set; }
    }
}
